package com.ticket.sass.admin.business.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ticket.sass.admin.business.HallBusiness;
import com.ticket.sass.admin.dto.input.hall.HallListInput;
import com.ticket.sass.admin.dto.input.hall.HallUpdInput;
import com.ticket.sass.admin.dto.output.hall.HallListOutput;
import com.ticket.sass.admin.model.PageModel;
import com.ticket.sass.admin.service.impl.HallServiceImpl;
import com.ticket.sass.admin.util.CinemaIdUtil;
import com.ticket.sass.admin.util.EscapeUtil;
import com.ticket.sass.admin.util.ModelUtil;
import com.ticket.sass.admin.util.ScheduleDataUtil;
import com.ticket.sass.common.entity.tenant.HallEntity;
import com.ticket.sass.common.exception.enums.ErrorCode;
import com.ticket.sass.common.exception.exception.BadRequestException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ywb
 * @date 2022-09-26 11:31
 */
@Slf4j
@Service
@AllArgsConstructor
public class HallBusinessImpl implements HallBusiness {
    final ModelUtil modelMapper;
    final HallServiceImpl hallService;
    final ScheduleDataUtil scheduleDataUtil;
    
    @Override
    public PageModel<HallListOutput> list(HallListInput input) {
        Long cinemaId = CinemaIdUtil.getCinemaIdFromRequest();
        
        IPage<HallEntity> page = hallService.iPage(input, cinemaId);
        List<HallListOutput> outputs = modelMapper.listToList(
            page.getRecords(),
            new TypeToken<List<HallListOutput>>(){}.getType()
        );
        return new PageModel<>(page.getTotal(), outputs);
    }
    
    @Override
    public void upd(HallUpdInput input) {
        if (hallService.isEmpty(input.getId())){
            throw new BadRequestException(
                ErrorCode.RECORD_NOT_EXIST,
                EscapeUtil.errorMessage("id", "记录不存在")
            );
        }
        
        HallEntity hallEntity = modelMapper.mapToMap(input, HallEntity.class);
        hallService.updateById(hallEntity);
    }
    
    @Override
    public void sync() {
        Long cinemaId = CinemaIdUtil.getCinemaIdFromRequest();
        
        // 同步过来的数据
        List<HallEntity> existingHalls = scheduleDataUtil.getHalls(cinemaId);
        // 获取所有编码
        List<String> codes = hallService.codes(cinemaId);
        
        // 分别存储新增和需要更新的影厅
        List<HallEntity> newHalls = new ArrayList<>();
        
        for (HallEntity hall : existingHalls) {
            hall.setSyncTime(LocalDateTime.now());
            // 检查同步过来的数据是否已经存在
            // 已经存在 更新座位数
            if (codes.contains(hall.getCode())){
                hallService.update(
                    new UpdateWrapper<HallEntity>()
                        .set("sync_time", hall.getSyncTime())
                        .set("seat_count", hall.getSeatCount())
                        .eq("code", hall.getCode())
                );
                // 不存在 新增数据
            } else {
                hall.setCinemaId(cinemaId);
                newHalls.add(hall);
            }
        }
        
        // 批量保存新增的影厅
        if (!newHalls.isEmpty()) {
            hallService.saveBatch(newHalls);
        }
    }
}
